<html>
<head><meta charset="utf-8"><title>const floating point methods · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html">const floating point methods</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236114044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236114044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236114044">(Apr 26 2021 at 03:04)</a>:</h4>
<p>Are all const floating point methods covered by <a href="https://github.com/rust-lang/rust/issues/57241"><code>const_fn_floating_point_arithmetic</code></a>? Not sure what feature gate it should go under. Either way, any objections to constifying a number of floating point methods (unstably of course)? I know there's unresolved issues regarding consistency, but it's nice to be able to play around a bit on nightly.</p>



<a name="236121319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236121319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236121319">(Apr 26 2021 at 05:11)</a>:</h4>
<p>There's been a lot of discussion around this, it's a bit of a quagmire.</p>
<p>CC <span class="user-mention" data-user-id="281757">@Jubilee</span>, <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="236122871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236122871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236122871">(Apr 26 2021 at 05:37)</a>:</h4>
<p>I believe it's for "floating point arithmetic in const fn". Individual methods on f32 and f64 <strong>may</strong> never qualify as const fn. I suggest if you want to draft the PR, pick a conservative set that seem easy to constify, add a new feature gate <strong>on top</strong> of <code>const_fn_floating_point_arithmetic</code>, and also please submit documentation clarifying this to the unstable book, before or at the same time as you add the documentation for the new unstable feature gate.</p>



<a name="236122993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236122993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236122993">(Apr 26 2021 at 05:39)</a>:</h4>
<p>By "conservative", I mean "if it's a transcendental function, it's probably not gonna happen, and certainly not right away."</p>



<a name="236123195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236123195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236123195">(Apr 26 2021 at 05:42)</a>:</h4>
<p>Alright, I'll check it out. Ralf knows more about the status of floats in MIRI and whether it's even currently possible to implement from a technical perspective, given that many are backed by intrinsics. The methods I was looking at were basic ones like <code>floor</code> and <code>fract</code>, though it's actually possible to write it manually on stable (although there's probably some subtleties I'm not aware of).</p>



<a name="236123451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236123451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236123451">(Apr 26 2021 at 05:46)</a>:</h4>
<p>Indeed, I don't know if it will be accepted or if Ralf thinks it's likely, it just sounds "reasonably plausible on nightly, probably a different feature gate".</p>



<a name="236135871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236135871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236135871">(Apr 26 2021 at 08:13)</a>:</h4>
<p>See also <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2337r0.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2337r0.pdf</a>, which is C++ grappling with this problem</p>



<a name="236161425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236161425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236161425">(Apr 26 2021 at 12:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/213817-t-lang/topic/const.20floating.20point.20methods/near/236121319">said</a>:</p>
<blockquote>
<p>There's been a lot of discussion around this, it's a bit of a quagmire.</p>
</blockquote>
<p>"quagmire" is a beautiful word that I did not know :D</p>



<a name="236161569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236161569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236161569">(Apr 26 2021 at 12:06)</a>:</h4>
<blockquote>
<p>any objections to constifying a number of floating point methods (unstably of course)?</p>
</blockquote>
<p>No, not really, as long as the tracking issue makes it extra clear that stabilization is blocked on <a href="https://github.com/rust-lang/rust/issues/57241">https://github.com/rust-lang/rust/issues/57241</a></p>



<a name="236161812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236161812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236161812">(Apr 26 2021 at 12:08)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281757">Jubilee</span> <a href="#narrow/stream/213817-t-lang/topic/const.20floating.20point.20methods/near/236122993">said</a>:</p>
<blockquote>
<p>By "conservative", I mean "if it's a transcendental function, it's probably not gonna happen, and certainly not right away."</p>
</blockquote>
<p>The CTFE engine currently does not support transcendental functions, and it is guaranteed to always use softfloats for everything. (It would be a critical bug if I had missed a case here...) So any library-only change is "safe" in the sense that it won't introduce a dependency on host floats.</p>



<a name="236162231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236162231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236162231">(Apr 26 2021 at 12:11)</a>:</h4>
<p>In my view, the way to support transcendental functions in CTFE is to have softfloat implementations of those functions that do achieve the best possible precision, i.e., their result is the same as "do the computation with arbitrary precision, then round to nearest floating-point-representable number".<br>
But even then we still have to figure out if we are okay with these results possibly being different than what the runtime libm will produce.</p>



<a name="236199823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236199823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236199823">(Apr 26 2021 at 15:58)</a>:</h4>
<p>Oh OK, cool, I didn't know if Miri was just Smart or Too Smart.</p>



<a name="236956955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/const%20floating%20point%20methods/near/236956955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/const.20floating.20point.20methods.html#236956955">(May 01 2021 at 10:38)</a>:</h4>
<p><a href="https://github.com/rust-lang/miri/">Miri</a> is probably Too Smart but we kept that out of the CTFE engine ;)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>